//----------------------------------------------------------------------------
// M A R I T I M E  R E S E A R C H  I N S T I T U T E  N E T H E R L A N D S
//----------------------------------------------------------------------------
// Copyright (C) 2012 - MARIN - All rights reserved - http://www.marin.nl
//----------------------------------------------------------------------------
// Program    : mermaid
// Module     : vbm
// File       : leapfrog_kernels.h
// Author     : M.A. de Jong
//----------------------------------------------------------------------------

#ifndef LEAPFROG_KERNELS_H
#define LEAPFROG_KERNELS_H

#ifdef USEDOUBLE
#define ONE 1.0
#define HALF 0.5
#else
#define ONE 1.0f
#define HALF 0.5f
#endif

namespace wavescomputer
{

template <class T>
__global__ void kernel_leapfrog(T* phi,
                                T* zeta,
                                const T* dt_phi,
                                const T* dt_zeta,
                                const T* zetam1,
                                const T* zetam2,
                                const T* phim1,
                                const T* phim2,
                                const T* alfa_brk,
                                const T dt_new,
                                const T dt_old,
                                const int ld,
                                const int Nx1,
                                const int Nx2)
{
    int u = bx * Bx + tx;
    int v = by * By + ty;
    int tid = v * ld + u;

    const T beta = dt_new / dt_old;
    const T fac1 = beta * beta;
    const T fac2 = 1 - fac1;
    const T fac3 = (beta + ONE) * dt_new;
    const T fac4 = HALF * dt_new;

    const T val_alfa_brk = alfa_brk[tid];
    
    zeta[tid] = fac1 * zetam2[tid] + 
                fac2 * zetam1[tid] +
                fac3 * dt_zeta[tid];
    
    phi[tid] = (fac1 * phim2[tid] + (fac2 - fac4 * val_alfa_brk) * phim1[tid] 
                    + fac3 * dt_phi[tid]) / (ONE + fac4 * val_alfa_brk);
}

} // namespace wavescomputer

#endif // LEAPFROG_KERNELS_H

